package com.example.home.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


import com.bumptech.glide.Glide;
import com.example.home.activity.R;
import com.example.home.viewmodel.home.Channel;

import java.util.List;

public class ChannelAdapter extends BaseAdapter {

    private Context ctx;
    private List<Channel> channel;

    public ChannelAdapter(Context ctx,List<Channel> channel){
        this.channel = channel;
        this.ctx = ctx;
    }
    @Override
    public int getCount() {
        return this.channel.size();
    }

    @Override
    public Object getItem(int position) {
        return this.channel.get(position);
    }

    @Override
    public long getItemId(int index) {
        return index;
    }

    @Override
    public View getView(int index, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView == null){
            convertView = LayoutInflater.from(this.ctx).inflate(R.layout.home_channel_item,null);
            viewHolder = new ViewHolder();
            viewHolder.home_channel_img = convertView.findViewById(R.id.home_channel_img);
            viewHolder.home_channel_txt = convertView.findViewById(R.id.home_channel_txt);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.home_channel_txt.setText(this.channel.get(index).getName());
        Glide.with(convertView.getContext())
                .load(this.channel.get(index).getIconUrl())
                .into(viewHolder.home_channel_img);

        return convertView;
    }

    static class ViewHolder {
        public ImageView home_channel_img;
        public TextView home_channel_txt;
    }
}
